package com.wuliu.servlet;


import com.wuliu.dao.NewsClassDAO;
import com.wuliu.dao.NewsDAO;
import com.wuliu.dao.impl.NewsClassDAOImpl;
import com.wuliu.dao.impl.NewsDAOImpl;
import com.wuliu.model.News;
import com.wuliu.model.NewsClass;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/page6")
public class NewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsClassDAO newsClassDAO = new NewsClassDAOImpl();
        List<NewsClass> newsClasses = newsClassDAO.findAll();
        req.setAttribute("newsClasses", newsClasses);

        NewsDAO newsDAO=new NewsDAOImpl();
        String cid=req.getParameter("newsClassId");
        if (cid==null){
            List<News> newsList=newsDAO.findAll();
            req.setAttribute("newsList",newsList);
        }else{
            List<News> newsList=newsDAO.findByCid(Integer.valueOf(cid));
            req.setAttribute("newsList",newsList);
        }

        req.getRequestDispatcher("/WEB-INF/view/news.jsp").forward(req,resp);


    }
}
